package com.niiwoo.civet.user.service.local;

import com.niiwoo.civet.user.dao.entity.IdentityInfo;
import com.niiwoo.civet.user.dao.entity.InviteRegister;
import com.niiwoo.civet.user.dao.entity.UserBasicInfo;
import com.niiwoo.civet.user.dao.mapper.IdentityInfoMapperExt;
import com.niiwoo.civet.user.dao.mapper.InviteRegisterMapperExt;
import com.niiwoo.civet.user.dao.mapper.UserBasicInfoMapperExt;
import com.niiwoo.civet.user.dto.response.BeInviterUserInfoRespDTO;
import com.niiwoo.civet.user.dto.response.InviterUserInfoRespDTO;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @Author: wuke
 * @Description:
 * @Date: Created in 15:15 2018/3/26
 */
@Slf4j
@Service
public class UserInviteRegisterService {
    @Autowired
    InviteRegisterMapperExt inviteRegisterMapper;
    @Autowired
    IdentityInfoMapperExt identityInfoMapper;
    @Autowired
    UserBasicInfoMapperExt userBasicInfoMapper;

    /**
     * 根据被邀请人用户ID查询邀请人信息
     * @param userId
     * @return
     */
    public InviterUserInfoRespDTO queryInviterUserInfo(String userId){
        InviteRegister inviteRegister = inviteRegisterMapper.selectByPrimaryKey(userId);
        if(inviteRegister==null){
            return null;
        }
        String inviterUserId = inviteRegister.getInviterUserId();
        UserInfo inviterUserInfo = this.queryUserInfo(inviterUserId);
        InviterUserInfoRespDTO respDTO = new InviterUserInfoRespDTO();
        respDTO.setInviterUserId(inviterUserId);
        respDTO.setInviteTime(inviteRegister.getCreateTime());
        respDTO.setInviterName(inviterUserInfo.getName());
        respDTO.setInviterMobileNo(inviterUserInfo.getMobileNo());
        return respDTO;
    }

    /**
     * 查询被邀请人信息列表
     * @param userId
     * @return
     */
    /*public List<BeInviterUserInfoRespDTO> queryBeInviterUserInfo(String userId){
        List<InviteRegister> inviteRegisterList = inviteRegisterMapper.selectByInviterUserId(userId);
        if(CollectionUtils.isEmpty(inviteRegisterList)){
            return null;
        }
        List<BeInviterUserInfoRespDTO> beInviterUserInfoRespDTOList = new ArrayList<>();
        inviteRegisterList.forEach(ir->{
            BeInviterUserInfoRespDTO beInviterUserInfoRespDTO = new BeInviterUserInfoRespDTO();
            String beInviterUserId = ir.getUserId();
            UserInfo beInviterUserInfo = this.queryUserInfo(beInviterUserId);

            beInviterUserInfoRespDTO.setBeInviterUserId(beInviterUserId);
            beInviterUserInfoRespDTO.setBeInviteTime(ir.getCreateTime());
            beInviterUserInfoRespDTO.setBeInviterName(beInviterUserInfo.getName());
            beInviterUserInfoRespDTO.setBeInviterMobileNo(beInviterUserInfo.getMobileNo());

            beInviterUserInfoRespDTOList.add(beInviterUserInfoRespDTO);
        });
        return beInviterUserInfoRespDTOList;
    }*/

    /**
     * 查询被邀请人信息列表
     * @param userId
     * @return
     */
    public List<BeInviterUserInfoRespDTO> queryBeInviterUserInfo(String userId){
        List<InviteRegister> inviteRegisterList = inviteRegisterMapper.selectByInviterUserId(userId);
        if(CollectionUtils.isEmpty(inviteRegisterList)){
            return null;
        }

        Set<String> inviterUserIdSet = this.getInviterUserIdSet(inviteRegisterList);

        List<IdentityInfo> identityInfoList = identityInfoMapper.queryUserIdentityListByUserIdSet(inviterUserIdSet);
        List<UserBasicInfo> userBasicInfoList = userBasicInfoMapper.queryListByUserIdSet(new ArrayList<>(inviterUserIdSet));

        List<BeInviterUserInfoRespDTO> beInviterUserInfoRespDTOList = new ArrayList<>();
        inviteRegisterList.forEach(ir->{
            BeInviterUserInfoRespDTO beInviterUserInfoRespDTO = new BeInviterUserInfoRespDTO();
            String beInviterUserId = ir.getUserId();
            UserInfo beInviterUserInfo = this.queryUserInfo(beInviterUserId,identityInfoList,userBasicInfoList);

            beInviterUserInfoRespDTO.setBeInviterUserId(beInviterUserId);
            beInviterUserInfoRespDTO.setBeInviteTime(ir.getCreateTime());
            beInviterUserInfoRespDTO.setBeInviterName(beInviterUserInfo.getName());
            beInviterUserInfoRespDTO.setBeInviterMobileNo(beInviterUserInfo.getMobileNo());

            beInviterUserInfoRespDTOList.add(beInviterUserInfoRespDTO);
        });
        return beInviterUserInfoRespDTOList;
    }

    private Set<String> getInviterUserIdSet(List<InviteRegister> inviteRegisterList){
        Set<String> inviterUserIdSet = new HashSet<>();
        inviteRegisterList.forEach(ir->{
            inviterUserIdSet.add(ir.getUserId());
        });
        return inviterUserIdSet;
    }

    private UserInfo queryUserInfo(String userId,List<IdentityInfo> identityInfoList,List<UserBasicInfo> userBasicInfoList){
        UserInfo userInfo = new UserInfo();

        Optional.ofNullable(identityInfoList).ifPresent(list->{
            for(IdentityInfo identityInfo : list){
                if(Objects.equals(userId,identityInfo.getUserId())) {
                    userInfo.setName(identityInfo.getName());
                    break;
                }
            }
        });

        Optional.ofNullable(userBasicInfoList).ifPresent(list->{
            for(UserBasicInfo userBasicInfo : list){
                if(Objects.equals(userId,userBasicInfo.getUserId())) {
                    userInfo.setMobileNo(userBasicInfo.getMobileNo());
                    break;
                }
            }
        });

        return userInfo;
    }

    private UserInfo queryUserInfo(String userId){
        UserInfo userInfo = new UserInfo();
        IdentityInfo identityInfo = identityInfoMapper.selectByPrimaryKey(userId);
        if(identityInfo!=null){
            userInfo.setName(identityInfo.getName());
        }
        UserBasicInfo userBasicInfo = userBasicInfoMapper.selectByPrimaryKey(userId);
        if(userBasicInfo!=null){
            userInfo.setMobileNo(userBasicInfo.getMobileNo());
        }
        return userInfo;
    }

    @Data
    private class UserInfo{
        private String mobileNo;
        private String name;
    }
}
